package com.archeanx.libx.http;

import android.util.ArrayMap;


import org.json.JSONObject;

import okhttp3.RequestBody;

/**
 * 以json  请求封装
 *
 * @author xz
 */
public class XHttpBodyHelper {

    private ArrayMap<String, Object> mParamsMap = new ArrayMap<>();

    public ArrayMap<String, Object> getParamsMap() {
        return mParamsMap;
    }

    public void setParamsMap(ArrayMap<String, Object> paramsMap) {
        mParamsMap = paramsMap;
    }

    public static XHttpBodyHelper newBuilder() {
        return new XHttpBodyHelper();
    }

    public XHttpBodyHelper addParam(String key, Object obj) {
        mParamsMap.put(key, obj);
        return this;
    }

    public XHttpBodyHelper addParams(ArrayMap<String, Object> params) {
        mParamsMap.putAll(params);
        return this;
    }

    public RequestBody build() {
        return RequestBody.create(GsonUtil.getGSON().toJson(mParamsMap), okhttp3.MediaType.parse("application/json; charset=utf-8")
        );
    }


    public static RequestBody emptyBuild() {
        return RequestBody.create(new JSONObject().toString(), okhttp3.MediaType.parse("application/json; charset=utf-8")
        );
    }

    public static RequestBody objBuild(Object obj) {
        return RequestBody.create(GsonUtil.getGSON().toJson(obj), okhttp3.MediaType.parse("application/json; charset=utf-8")
        );
    }
}
